package goods.controller;

import goods.service.IGoodsService;
import common.model.result.Result;
import common.model.goods.Category;
import common.model.goods.Sku;
import common.model.goods.Spu;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import common.dto.goodsdto.GoodsDTO;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    IGoodsService goodsService;
    //前台
    // 根据商铺id查询商品列表
    @GetMapping("/list")
    public Result list(
        @RequestParam("shopId") Long shopId,//商铺id
        @RequestParam("sort") String sort,//排序
        @RequestParam("page") Integer page,//页码
        @RequestParam("pageSize") Integer pageSize//每页条数
        ) {
        return goodsService.findByShopId(shopId, sort, page, pageSize);
    }
    // 根据商品id查询商品详情
    @GetMapping("/detail")
    public Result detail(@RequestParam(value = "id", required = true) Long id) {
        return goodsService.findGoodsById(id);
    }
    // 根据商品spuId查询spu详情
    @GetMapping("/spu/{id}")
    public Result spuDetail(@PathVariable("id") Long id) {
        return goodsService.findSpu(id);
    }

    // 根据skuId查询sku商品
    @GetMapping("/sku/{id}")
    public Result skuDetail(@PathVariable("id") Long id) {
        return goodsService.findSku(id);
    }

    // 根据商品id查询商品属性参数信息
    @GetMapping("/attr")
    public Result attribute(@RequestParam(value = "id") Long id) {
        return goodsService.findAttribute(id);
    }

    // 推荐商品
    @GetMapping("/recommend")
    public Result recommend() {
        return goodsService.recommend();
    }

    //获取分类信息
    @GetMapping("/category")
    public Result category() {
        return goodsService.getCategory();
    }

    //搜索商品
    @GetMapping("/search")
    public Result search(@RequestParam("data") String data, @PageableDefault(page = 0, size = 10) Pageable pageable) {
        return goodsService.search(data, pageable);
    }

    //更新sku库存
    @PutMapping("/updateSkuStock")
    public Result updateSkuStock(@RequestBody Sku sku) {
        return goodsService.updateSkuStock(sku);
    }

    //后台
    //获取商品分类占比
    @GetMapping("/jiang/categoryPercent")
    public Result getCategoryPercent() {
        return goodsService.getCategoryPercent();
    }
    //获取商品列表
    @GetMapping("/jiang/list")
    public Result getGoodsList(
        @RequestParam(value = "page", defaultValue = "1") Integer page,
        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        return goodsService.getGoodsList(page, pageSize);
    }
    //新增商品
    @PostMapping("/jiang/add")
    public Result add(@RequestBody GoodsDTO goods) {//goods包含spu和skuList内容
        return goodsService.add(goods);
    }
    // 新增商品sku
    @PostMapping("/jiang/addSku")
    public Result addSku(@RequestBody Sku sku) {
        return goodsService.addSku(sku);
    }
    // 修改商品spu
    @PutMapping("/jiang/updateSpu")
    public Result updateSpu(@RequestBody Spu spu) {
        return goodsService.updateSpu(spu);
    }
    // 修改商品sku
    @PutMapping("/updateSku")
    public Result updateSku(@RequestBody Sku sku) {
        return goodsService.updateSku(sku);
    }
    // 删除商品
    @DeleteMapping("/jiang/delete")
    public Result delete(@RequestParam("id") Long id) {
        return goodsService.delete(id);
    }
    // 删除商品sku  
    @DeleteMapping("/jiang/deleteSku")
    public Result deleteSku(@RequestParam("id") Long id) {
        return goodsService.deleteSku(id);
    }
    //添加分类
    @PostMapping("/jiang/addCategory")
    public Result addCategory(@RequestBody Category category) {
        return goodsService.addCategory(category);
    }
    //更新分类
    @PutMapping("/jiang/updateCategory")
    public Result updateCategory(@RequestBody Category category) {
        return goodsService.updateCategory(category);
    }
    //删除分类
    @PostMapping("/jiang/deleteCategory")
    public Result deleteCategory(@RequestParam("id") Long id) {
        return goodsService.deleteCategory(id);
    }
    // 获取所有二级分类
    @GetMapping("/jiang/secondCategory")
    public Result getSecondCategory() {
        return goodsService.getSecondCategory();
    }
    //获取分类列表
    @GetMapping("/category/list")
    public Result getCategoryList() {
        return goodsService.getCategoryList();
    }
    //修改商品上下架状态
    @PutMapping("/jiang/updateSpuStatus")
    public Result updateSpuStatus(@RequestParam("id") Long id, @RequestParam("status") Boolean status) {
        return goodsService.updateSpuStatus(id, status);
    }
}
